﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace BYManageInfoSystem
{
    public partial class Frm_HaiGuan : DockContent
    {
        private DGBY.BLL.HaiGuanTab m_bllHaiGuanTab = new DGBY.BLL.HaiGuanTab();
        private DataSet m_ds = new DataSet();
        private int m_nSelectPortID = 0;

        public Frm_HaiGuan()
        {
            InitializeComponent();
        }

        private void ShowAllRecordInDataGridView()
        {
            dataGridViewPortList.DataSource = null;
            m_ds = m_bllHaiGuanTab.GetAllList();
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dataGridViewPortList.DataSource = m_ds.Tables[0];
                dataGridViewPortList.Columns[0].Visible = false;
            }
        }

        private void buttonShowAllRecordPort_Click_1(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView();
        }

        private void dataGridViewPortList_CellMouseClick_1(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strPortID = this.dataGridViewPortList.Rows[this.dataGridViewPortList.CurrentCell.RowIndex].Cells[0].Value.ToString();
            m_nSelectPortID = Convert.ToInt32(strPortID, 10);
            cellclick();
        }

        private void cellclick()
        {
            DGBY.Model.HaiGuanTab modelHaiGuanTab = m_bllHaiGuanTab.GetModel(m_nSelectPortID);
            if (modelHaiGuanTab != null)
            {
                this.textBoxPort.Text = modelHaiGuanTab.HaiGuanName;
                this.textBoxHaiGuanAbbr.Text = modelHaiGuanTab.HaiGuanNameJX;
            }
            else
            {
                this.textBoxPort.Text = "";
                this.textBoxHaiGuanAbbr.Text = "";
            }
        }

        private void dataGridViewPortList_RowPostPaint_1(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            {
                using (SolidBrush b = new SolidBrush(dataGridViewPortList.RowHeadersDefaultCellStyle.ForeColor))
                {
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
                }
            }
        }

        private void buttonAddPort_Click_1(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.HaiGuanTab modelHaiGuanTab = new DGBY.Model.HaiGuanTab();
                modelHaiGuanTab.HaiGuanName = this.textBoxPort.Text;
                modelHaiGuanTab.HaiGuanNameJX = this.textBoxHaiGuanAbbr.Text;


                int nResult = m_bllHaiGuanTab.Add(modelHaiGuanTab);

                if (nResult != 1)
                {
                    Frm_Main.Log2DB(this.Text, "增加", modelHaiGuanTab.HaiGuanName);

                    m_nSelectPortID = m_bllHaiGuanTab.GetMaxId() - 1;
                    ShowAllRecordInDataGridView();
                    //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

        }

        private void buttonModifyPort_Click_1(object sender, EventArgs e)
        {
            try
            {
                if (m_bllHaiGuanTab.GetModel(m_nSelectPortID) == null)
                {
                    MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                DGBY.Model.HaiGuanTab modelHaiGuanTab = m_bllHaiGuanTab.GetModel(m_nSelectPortID);
                modelHaiGuanTab.HaiGuanName = this.textBoxPort.Text;
                modelHaiGuanTab.HaiGuanNameJX = this.textBoxHaiGuanAbbr.Text;
                //modelHaiGuanTab.HaiGuanTabID = m_nSelectPortID;

                m_bllHaiGuanTab.Update(modelHaiGuanTab);

                Frm_Main.Log2DB(this.Text, "修改", modelHaiGuanTab.HaiGuanName);
                ShowAllRecordInDataGridView();
                MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

        }

        private void buttonDelPort_Click_1(object sender, EventArgs e)
        {
            if (m_bllHaiGuanTab.GetModel(m_nSelectPortID) == null)
            {
                MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确实要删除该记录么，删除该记录可能会连带删除其他表记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            m_bllHaiGuanTab.Delete(m_nSelectPortID);
            ShowAllRecordInDataGridView();

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            cellclick();
        }

        private void Frm_HaiGuan_Load(object sender, EventArgs e)
        {
            if (Frm_Main.m_strLoginUser == "管理员")
            {
                this.buttonDelPort.Visible = true;
            }
        }




    }
}
